package com.gwtjs.icustom.controller.manager;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.gwtjs.icustom.entity.ResultWrapper;
import com.gwtjs.icustom.mapper.UserMapper;
import com.gwtjs.icustom.pojo.User;
import com.gwtjs.icustom.utils.ImportExcelUtil;

@RestController
@RequestMapping("/user")
public class ManagerUserController {

	@Autowired
	private UserMapper userMapper;
	@Resource
	private ImportExcelUtil excelUtils;

	private final static Logger logger = LoggerFactory.getLogger(ManagerUserController.class);

	@GetMapping("/queryUserList")
	public ResultWrapper queryUserList() {
		List<User> userList = userMapper.queryUserList();

		for (User user : userList) {
			System.out.println(user);
		}

		return ResultWrapper.successResult(userList);
	}

	@GetMapping("/{id}")
	public ResultWrapper queryUserById(@PathVariable("id") Integer id) {
		User user = userMapper.queryUserById(id);
		logger.info("\nadd user:", user.toString());
		return ResultWrapper.successResult(user);

	}

	/**
	 * @apiNote ":8080/user/add" { "account": "flash8627", "password": "flash8627",
	 *          "username": "flash8627", "address": "flash8627" }
	 * @param user
	 * @return
	 */
	@PostMapping("/add")
	public ResultWrapper addUser(@RequestBody List<User> records) {
		logger.info("\nadd user:", records.toString());
		try {
			for (User user : records) {
				userMapper.addUser(user);
			}
			return ResultWrapper.successResult(records);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.constructErrorResult(e.getMessage(), null);
		}

	}

	/**
	 * @apiNote ":8080/user/update" { "id":3, "account": "old_8627", "password":
	 *          "flash8627old", "username": "flash8627old", "address":
	 *          "flash8627old" }
	 * @param user
	 * @return
	 */
	@PutMapping("/update")
	public ResultWrapper updateUser(@RequestBody List<User> records) {
		logger.info("\nupdate user:", records.toString());
		try {
			for (User user : records) {
				userMapper.updateUser(user);
			}
			return ResultWrapper.successResult(records);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.constructErrorResult(e.getMessage(), null);
		}
		
	}

	/**
	 * @apiNote ":8080/user/delete" [{"id":1},{"id":3}]
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/delete")
	public ResultWrapper batchDeleteUser(@RequestBody List<User> records) {
		logger.info("\ndelete user:", records.toString());
		userMapper.deleteUser(records);
		return ResultWrapper.successResult(records);
	}

	@PostMapping("/upload")
	public Map<String, String> uploadUser(MultipartFile excelFile) throws Exception {
		Map<String, String> map = new HashMap<>();
		if (excelFile.isEmpty()) {
			map.put("mag", "文件夹为空，重新上传");
		} else {
			String fileName = excelFile.getOriginalFilename();
			InputStream is = excelFile.getInputStream();
			Workbook workbook = excelUtils.getWorkbook(fileName, is);
			List<User> userList = excelUtils.excelToUserObject(workbook);
			System.out.println(userList);

		}

		return map;
	}

}
